setTimeout(() => {
  console.log('111')
}, 1000)

let p = new Promise((resolve, reject) => {
  console.log('imooc')
  resolve('成功')
})
  .then((res) => {
    console.log(res)
  })
  .catch((err) => {
    console.log(err)
  })

// pending => rejected || fulfilled  只有3种状态 状态改变不可逆

new Promise((resolve, reject) => {
  ajax(url, (res) => {
    console.log(res)
    resolve()
  }).then((res) => {
    console.log('a成功')
    return new Promise((resolve, reject) => {
      ajax(url, (res) => {
        console.log(res)
        resolve()
      })
    }).then((res) => {
      console.log(res)
      return new Promise((resolve, reject) => {
        // ...
      })
    })
  })
})

//改造
function getPormise(url) {
  return new Promise((resolve, reject) => {
    ajax(url, (res) => {
      resolve(res)
    })
  })
}

getPormise(url1).then((res) => {
  console.log(res)
  return getPormise(url2)
}).then(res => {
  console.log(res);
  return getPormise(url3)
}).then((res) => {
  console.log(res)
})


//当 url1 失败后
getPormise(url1).then((res) => {
  console.log(res);
  return getPormise(url2)
}, err=> { // getPromise(url1) 失败后 进入err,err这里没有发送请求,所以 .then后面是undefined
  console.log(err);
  // return getPormise(url2)  要想仍然返回 promise2 这里再写一遍
}).then(res => {
  console.log(res); // 上一级没有返回任何东西,所以得到undefined
  return getPormise(url3) //这里再次请求成功
}).then(res => {
  console.log(res);//得到...
})


// 不管 url1 url2 url3 谁请求失败,可以在最后添加catch 进行统一的管理,第一个失败则直接进入catch

